<?php

namespace app\common\logic;



use app\common\model\shop\Order;
use app\common\model\User;
use app\common\utils\Utils;

/**
 * Token接口
 */
class UserLogic
{

    //用户id
    protected $userId = 0;
    //模型
    protected $user = null;
    protected $where = [];
    //下级id
    protected $children = [];
    public function __construct($userId)
    {
        $this->userId = $userId;
        $this->user = new User();
    }

    /**
     * 设置user_id
     * @param int $userId
     */
    public function setUid($userId) {
        $this->userId = $userId;
    }

    /**
     * 获取下级id
     * @return  \app\common\logic\UserLogic
     */
    public function getChildren() {
        $ids = $this->user->where($this->where)->where('parent_id', $this->userId)->column('id');
        $this->children = $ids;
        return $this;
    }

    /**
     * 获取我的业绩
     */
    public function getPerformance($where = []) {
        $order = new Order();
        $where['user_id'] = ['in', $this->children];
        $where['type'] = 1;
        $list = $order->where($where)->order('createtime desc')->paginate()->each(function($item, $key) {
        });
        return $list;
    }

    /**
     * 获取总业绩
     */
    public function getAllPerformance($where = []) {
        $order = new Order();
        $where['user_id'] = ['in', $this->children];
        $where['type'] = 1;
        $sum = $order->where($where)->sum('money');

        return $sum;
    }
}
